package org.black.phoneUtility.sms.reply.app.fragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumber;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumberFromContact;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumberFromContacts;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumberFromMatchSmsWithKeyword;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumberFromNumbersFromContactGroup;
import org.black.phoneUtility.sms.reply.database.dataModel.NoneReplyNumberFromUserInput;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyTimesMode;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyTimesModeAlways;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyTimesModeLimitedTimes;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumber;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumberFromContact;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumberFromNumbersFromContactGroup;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumberFromUserInput;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFrom;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromAllNumbers;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromAllNumbersInMyContacts;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromAllNumbersNotInMyContacts;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromChooseFromMyContacts;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromMatchSmsWithKeyword;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromNumbersFromContactGroup;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromUserInput;
import org.black.phoneUtility.sms.reply.database.dataModel.SmsReply;
import org.black.phoneUtility.sms.reply.provider.SmsReplyContract;
import org.black.phoneUtility.sms.reply.service.SmsReplyService;
import org.black.phoneUtility.sms.reply.util.DateTimeUtil;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.OperationApplicationException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RemoteException;
import android.telephony.PhoneNumberUtils;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SmsReplyAddEditFragment extends Fragment {
	public static final String TAG = SmsReplyAddEditFragment.class.getName();
	private static final String LOG_TAG = SmsReplyAddEditFragment.class
			.getName();
	public static final String ARGUMENTS_SMS_REPLY_ID = "smsReplyId";

	private SmsReplyService smsReplyService;
	private SmsReply smsReply;

	private class LoadSmsReplyAsyncTask extends AsyncTask<Long, Void, SmsReply> {

		@Override
		protected SmsReply doInBackground(Long... params) {
			getActivity().getContentResolver().notifyChange(
					SmsReplyContract.SmsReplies.CONTENT_URI, null);
			Long smsReplyId = params[0];
			SmsReply result = null;

			if (smsReply == null) {
				if (smsReplyId == 0) {
					result = new SmsReply();
					result.setTitle(getString(R.string.sms_reply_add_edit_title_default_value));

					result.setShouldResponseToMissedCall(true);
					result.setShouldResponseToSms(true);

					result.setReplyDateTimeModeType(SmsReply.REPLY_DATE_TIME_MODE_ALWAYS_TYPE);
					result.setExpiredAfterMilliSeconds(1000l * 60 * 30);
					Calendar startCalendar = Calendar.getInstance();
					Calendar endCalendar = Calendar.getInstance();
					endCalendar.add(Calendar.HOUR_OF_DAY, 1);
					result.setPeriodStartDateTime(startCalendar.getTime());
					result.setPeriodEndDateTime(endCalendar.getTime());
					Calendar repeatStartCalendar = Calendar.getInstance();
					Calendar repeatEndCalendar = Calendar.getInstance();
					repeatEndCalendar.add(Calendar.HOUR_OF_DAY, 1);
					result.setRepeatStartTime(repeatStartCalendar.getTime());
					result.setRepeatEndTime(repeatEndCalendar.getTime());
					result.setSundayEnable(false);
					result.setMondayEnable(true);
					result.setTuesdayEnable(true);
					result.setWednesdayEnable(true);
					result.setThursdayEnable(true);
					result.setFridayEnable(true);
					result.setSaturdayEnable(false);

					result.setMissedCallAndSmsUsedDifferentMessages(false);

					result.setPhoneMessage(getString(R.string.sms_reply_add_edit_message_reply_message_default));
					result.setSmsMessage(getString(R.string.sms_reply_add_edit_message_reply_message_default));

					/* Start */
					// TODO: New Column For SMS Reply
					/* End */
					result.setNotifyAfterSendingMessage(true);

					result.setReplyMode(SmsReply.REPLY_MODE_VALUE_REPLY_DIRECTLY);
					List<ResponseNumberFrom> responseNumberFroms = new ArrayList<ResponseNumberFrom>();
					ResponseNumberFromAllNumbersInMyContacts responseNumberFromContacts = new ResponseNumberFromAllNumbersInMyContacts();
					responseNumberFromContacts.setCreationDateTime(new Date());
					responseNumberFroms.add(responseNumberFromContacts);
					result.setResponseNumberFroms(responseNumberFroms);

					List<NoneReplyNumber> noneReplyNumbers = new ArrayList<NoneReplyNumber>();
					result.setNoneReplyNumbers(noneReplyNumbers);

					List<ReplyToSpecificNumber> replyToSpecificNumbers = new ArrayList<ReplyToSpecificNumber>();
					result.setReplyToSpecificNumbers(replyToSpecificNumbers);

					result.setReplyMode(SmsReply.REPLY_MODE_VALUE_REPLY_DIRECTLY);

					List<ReplyTimesMode> replyTimesModes = new ArrayList<ReplyTimesMode>();
					ReplyTimesModeAlways replyTimesModeAlways = new ReplyTimesModeAlways();
					replyTimesModeAlways.setActivationDateTime(new Date());
					replyTimesModeAlways.setIsActivated(true);
					ReplyTimesModeLimitedTimes replyTimesModeLimitedTimes = new ReplyTimesModeLimitedTimes();
					replyTimesModeLimitedTimes.setIsActivated(false);
					replyTimesModeLimitedTimes
							.setActivationDateTime(new Date());
					replyTimesModeLimitedTimes.setTimes(1);
					replyTimesModes.add(replyTimesModeAlways);
					replyTimesModes.add(replyTimesModeLimitedTimes);
					result.setReplyTimesModes(replyTimesModes);

				} else {
					result = smsReplyService.read(smsReplyId);
					result.setIsActivated(false);
				}
				smsReply = result;
			} else {
				result = smsReply;
			}

			return result;

		}

		@Override
		protected void onPostExecute(SmsReply result) {
			super.onPostExecute(result);
			View view = getView();
			if (view != null) {
				smsReply = result;
				EditText titleEditText = (EditText) view
						.findViewById(R.id.smsReplyAddEditTitleEditText);
				if (TextUtils.isEmpty(smsReply.getTitle()) == true) {
					titleEditText
							.setText(R.string.sms_reply_add_edit_title_default_value);
				} else {
					titleEditText.setText(smsReply.getTitle());
				}
				CheckBox responseToMissedCallCheckBox = (CheckBox) view
						.findViewById(R.id.smsReplyAddEditResponseToMissedCallCheckBox);
				responseToMissedCallCheckBox.setChecked(smsReply
						.getShouldResponseToMissedCall() == null ? false
						: smsReply.getShouldResponseToMissedCall());

				CheckBox responseToMSmsCheckBox = (CheckBox) view
						.findViewById(R.id.smsReplyAddEditResponseToSmsCheckBox);
				responseToMSmsCheckBox.setChecked(smsReply
						.getShouldResponseToSms() == null ? false : smsReply
						.getShouldResponseToSms());

				TextView responseDurationSettingSummaryTextView = (TextView) view
						.findViewById(R.id.smsReplyAddEditResponseDurationSettingSummaryTextView);

				if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_ALWAYS_TYPE) {
					responseDurationSettingSummaryTextView
							.setText(R.string.sms_reply_add_edit_date_time_summary_always);
				} else if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_EXPIRED_AFTER_TYPE) {

					long hours = (smsReply.getExpiredAfterMilliSeconds() / 1000l / 60 / 60);
					long minutes = (smsReply.getExpiredAfterMilliSeconds() % (1000l * 60 * 60)) / 60000;
					responseDurationSettingSummaryTextView
							.setText(getString(
									R.string.sms_reply_add_edit_date_time_summary_expired_after,
									hours < 10 ? "0" + hours : hours,
									minutes < 10 ? "0" + minutes : minutes));
				} else if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_PERIOD_TYPE) {
					responseDurationSettingSummaryTextView
							.setText(getString(
									R.string.sms_reply_add_edit_date_time_summary_period,
									DateFormat.getDateFormat(getActivity())
											.format(smsReply
													.getPeriodStartDateTime()),
									DateFormat.getTimeFormat(getActivity())
											.format(smsReply
													.getPeriodStartDateTime()),
									DateFormat.getDateFormat(getActivity())
											.format(smsReply
													.getPeriodEndDateTime()),
									DateFormat.getTimeFormat(getActivity())
											.format(smsReply
													.getPeriodEndDateTime())));
				} else if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_REPEAT_TYPE) {
					StringBuilder stringBuilder = new StringBuilder();
					if (DateTimeUtil.isNeverRepeat(smsReply) == true) {
						stringBuilder.append(getString(R.string.never));
					} else if (DateTimeUtil.isAlwaysRepeat(smsReply) == true) {
						stringBuilder.append(getString(R.string.everyday));
					} else {
						boolean isAppended = false;
						String comma = getActivity().getString(R.string.comma);
						if (smsReply.isSundayEnable() == true) {
							if (isAppended == false) {
								isAppended = true;
							} else {
								stringBuilder.append(comma);
							}
							stringBuilder
									.append(getString(R.string.sunday_abbreviation));
						}
						if (smsReply.isMondayEnable() == true) {
							if (isAppended == false) {
								isAppended = true;
							} else {
								stringBuilder.append(comma);
							}
							stringBuilder
									.append(getString(R.string.monday_abbreviation));

						}
						if (smsReply.isTuesdayEnable() == true) {
							if (isAppended == false) {
								isAppended = true;
							} else {
								stringBuilder.append(comma);
							}
							stringBuilder
									.append(getString(R.string.tuesday_abbreviation));

						}
						if (smsReply.isWednesdayEnable() == true) {
							if (isAppended == false) {
								isAppended = true;
							} else {
								stringBuilder.append(comma);
							}
							stringBuilder
									.append(getString(R.string.wednesday_abbreviation));

						}
						if (smsReply.isThursdayEnable() == true) {
							if (isAppended == false) {
								isAppended = true;
							} else {
								stringBuilder.append(comma);
							}
							stringBuilder
									.append(getString(R.string.thursday_abbreviation));
						}
						if (smsReply.isFridayEnable() == true) {
							if (isAppended == false) {
								isAppended = true;
							} else {
								stringBuilder.append(comma);
							}
							stringBuilder
									.append(getString(R.string.friday_abbreviation));

						}
						if (smsReply.isSaturdayEnable() == true) {
							if (isAppended == false) {
								isAppended = true;
							} else {
								stringBuilder.append(comma);
							}
							stringBuilder
									.append(getString(R.string.saturday_abbreviation));

						}
					}
					responseDurationSettingSummaryTextView
							.setText(getString(
									R.string.sms_reply_add_edit_date_time_summary_repeat,
									DateFormat.getTimeFormat(getActivity())
											.format(smsReply
													.getRepeatStartTime()),
									DateFormat
											.getTimeFormat(getActivity())
											.format(smsReply.getRepeatEndTime()),
									stringBuilder.toString()));
				}

				TextView smsReplyAddEditReplyTimesSummaryTextView = (TextView) view
						.findViewById(R.id.smsReplyAddEditReplyTimesSummaryTextView);
				for (ReplyTimesMode replyTimesMode : smsReply
						.getReplyTimesModes()) {
					if (replyTimesMode.getIsActivated() == true) {
						if (replyTimesMode instanceof ReplyTimesModeAlways) {
							smsReplyAddEditReplyTimesSummaryTextView
									.setText(R.string.sms_reply_add_edit_reply_times_always_reply);
						} else if (replyTimesMode instanceof ReplyTimesModeLimitedTimes) {
							ReplyTimesModeLimitedTimes replyTimesModeLimitedTimes = (ReplyTimesModeLimitedTimes) replyTimesMode;
							Integer replyTimes = replyTimesModeLimitedTimes
									.getTimes();
							if (null == replyTimes) {
								replyTimesModeLimitedTimes.setTimes(5);
								replyTimes = ((ReplyTimesModeLimitedTimes) replyTimesMode)
										.getTimes();
							}
							smsReplyAddEditReplyTimesSummaryTextView
									.setText(getResources()
											.getQuantityString(
													R.plurals.sms_reply_add_edit_reply_times_limited_times,
													replyTimes, replyTimes));
						}
					}
				}

				CheckBox silentModeCheckBox = (CheckBox) view
						.findViewById(R.id.smsReplyAddEditSilentModeCheckBox);
				silentModeCheckBox.setChecked(smsReply
						.isEnableRingerModeSilent());
				CheckBox useDifferentMessageCheckBox = (CheckBox) view
						.findViewById(R.id.smsReplyAddEditUseDifferentMessageCheckBox);
				RelativeLayout allMessageRelativeLayout = (RelativeLayout) view
						.findViewById(R.id.smsReplyAddEditAllMessageRelativeLayout);
				TextView allMessageSummaryTextView = (TextView) view
						.findViewById(R.id.smsReplyAddEditAllMessageSummaryTextView);
				RelativeLayout messageMissedCallRelativeLayout = (RelativeLayout) view
						.findViewById(R.id.smsReplyAddEditMessageMissedCallRelativeLayout);
				TextView missedCallMessageSummaryTextView = (TextView) view
						.findViewById(R.id.smsReplyAddEditMessageMissedCallMessageSummaryTextView);
				RelativeLayout messageSmsRelativeLayout = (RelativeLayout) view
						.findViewById(R.id.smsReplyAddEditMessageSmsRelativeLayout);
				TextView smsMessageSummaryTextView = (TextView) view
						.findViewById(R.id.smsReplyAddEditMessageSmsMessageSummaryTextView);
				if (smsReply.getMissedCallAndSmsUsedDifferentMessages() != null
						&& smsReply.getMissedCallAndSmsUsedDifferentMessages() == true) {
					useDifferentMessageCheckBox.setChecked(true);
					allMessageRelativeLayout.setVisibility(View.GONE);
					messageMissedCallRelativeLayout.setVisibility(View.VISIBLE);
					messageSmsRelativeLayout.setVisibility(View.VISIBLE);
				} else {
					useDifferentMessageCheckBox.setChecked(false);
					allMessageRelativeLayout.setVisibility(View.VISIBLE);
					messageMissedCallRelativeLayout.setVisibility(View.GONE);
					messageSmsRelativeLayout.setVisibility(View.GONE);
				}
				allMessageSummaryTextView.setText(smsReply.getPhoneMessage());
				missedCallMessageSummaryTextView.setText(smsReply
						.getPhoneMessage());
				smsMessageSummaryTextView.setText(smsReply.getSmsMessage());

				CheckBox replyToSpecificNumberCheckBox = (CheckBox) view
						.findViewById(R.id.smsReplyAddEditReplyToSpecificNumberCheckBox);

				TextView numberFromSummaryTextView = (TextView) view
						.findViewById(R.id.smsReplyAddEditNumberFromSummaryTextView);

				StringBuilder numberFromNumberStringBuilder = new StringBuilder();
				if (smsReply.getResponseNumberFroms() == null
						|| smsReply.getResponseNumberFroms().size() == 0) {
					numberFromNumberStringBuilder
							.append(getString(R.string.none));
				} else {
					boolean isAppend = false;
					String comma = ", ";
					for (ResponseNumberFrom responseNumberFrom : smsReply
							.getResponseNumberFroms()) {
						if (isAppend == false) {
							isAppend = true;
						} else {
							numberFromNumberStringBuilder.append(comma);
						}
						if (responseNumberFrom instanceof ResponseNumberFromAllNumbers) {
							numberFromNumberStringBuilder
									.append(getString(R.string.sms_add_edit_response_number_from_number_from_all_numbers));
						} else if (responseNumberFrom instanceof ResponseNumberFromAllNumbersInMyContacts) {
							numberFromNumberStringBuilder
									.append(getString(R.string.sms_add_edit_response_number_from_number_from_all_numbers_in_contacts));
						} else if (responseNumberFrom instanceof ResponseNumberFromAllNumbersNotInMyContacts) {
							numberFromNumberStringBuilder
									.append(getString(R.string.sms_add_edit_response_number_from_number_from_all_numbers_not_in_contacts));
						} else if (responseNumberFrom instanceof ResponseNumberFromChooseFromMyContacts) {
							ResponseNumberFromChooseFromMyContacts responseNumberFromContact = (ResponseNumberFromChooseFromMyContacts) responseNumberFrom;
							numberFromNumberStringBuilder
									.append(responseNumberFromContact.getName()
											+ " ("
											+ PhoneNumberUtils
													.formatNumber(responseNumberFromContact
															.getPhoneNumber())
											+ ") ");
						} else if (responseNumberFrom instanceof ResponseNumberFromUserInput) {
							ResponseNumberFromUserInput responseNumberFromUserInput = (ResponseNumberFromUserInput) responseNumberFrom;
							numberFromNumberStringBuilder
									.append(PhoneNumberUtils
											.formatNumber(responseNumberFromUserInput
													.getPhoneNumber()));

						} else if (responseNumberFrom instanceof ResponseNumberFromMatchSmsWithKeyword) {
							ResponseNumberFromMatchSmsWithKeyword responseNumberFromMatchSmsWithKeyword = (ResponseNumberFromMatchSmsWithKeyword) responseNumberFrom;
							numberFromNumberStringBuilder
									.append(getString(R.string.sms_add_edit_response_number_from_number_from_sms_messages_contains_keyword_keyword)
											+ responseNumberFromMatchSmsWithKeyword
													.getKeyword());
						} else if (responseNumberFrom instanceof ResponseNumberFromNumbersFromContactGroup) {
							ResponseNumberFromNumbersFromContactGroup responseNumberFromNumbersFromContactGroup = (ResponseNumberFromNumbersFromContactGroup) responseNumberFrom;
							numberFromNumberStringBuilder
									.append(responseNumberFromNumbersFromContactGroup
											.getName()
											+ " ("
											+ PhoneNumberUtils
													.formatNumber(responseNumberFromNumbersFromContactGroup
															.getPhoneNumber())
											+ ") ");
						}
					}
				}
				numberFromSummaryTextView.setText(numberFromNumberStringBuilder
						.toString());
				TextView responseToSpecificNumbersSummaryTextView = (TextView) view
						.findViewById(R.id.smsReplyAddEditResponseToSpecificNumbersSummaryTextView);
				RelativeLayout reponseToSpecificNumberRelativeLayout = (RelativeLayout) view
						.findViewById(R.id.smsReplyAddEditResponseToSpecificNumbersRelativeLayout);
				if (smsReply.getReplyMode() == SmsReply.REPLY_MODE_VALUE_REPLY_DIRECTLY) {
					reponseToSpecificNumberRelativeLayout
							.setVisibility(View.GONE);
					replyToSpecificNumberCheckBox.setChecked(false);
				} else {
					reponseToSpecificNumberRelativeLayout
							.setVisibility(View.VISIBLE);
					replyToSpecificNumberCheckBox.setChecked(true);
				}

				StringBuilder replyToSpecificNumberStringBuilder = new StringBuilder();
				if (smsReply.getReplyToSpecificNumbers() == null
						|| smsReply.getReplyToSpecificNumbers().size() == 0) {
					replyToSpecificNumberStringBuilder
							.append(getString(R.string.none));
				} else {
					boolean isAppend = false;
					String comma = ", ";
					for (ReplyToSpecificNumber replyToSpecificNumber : smsReply
							.getReplyToSpecificNumbers()) {
						if (isAppend == false) {
							isAppend = true;
						} else {
							replyToSpecificNumberStringBuilder.append(comma);
						}
						if (replyToSpecificNumber instanceof ReplyToSpecificNumberFromContact) {
							ReplyToSpecificNumberFromContact replyToSpecificNumberFromContact = (ReplyToSpecificNumberFromContact) replyToSpecificNumber;
							replyToSpecificNumberStringBuilder
									.append(replyToSpecificNumberFromContact
											.getName()
											+ " ("
											+ PhoneNumberUtils
													.formatNumber(replyToSpecificNumberFromContact
															.getPhoneNumber())
											+ ") ");
						} else if (replyToSpecificNumber instanceof ReplyToSpecificNumberFromUserInput) {
							ReplyToSpecificNumberFromUserInput replyToSpecificNumberFromUserInput = (ReplyToSpecificNumberFromUserInput) replyToSpecificNumber;
							replyToSpecificNumberStringBuilder
									.append(PhoneNumberUtils
											.formatNumber(replyToSpecificNumberFromUserInput
													.getPhoneNumber()));

						} else if (replyToSpecificNumber instanceof ReplyToSpecificNumberFromNumbersFromContactGroup) {
							ReplyToSpecificNumberFromNumbersFromContactGroup replyToSpecificNumberFromNumbersFromContactGroup = (ReplyToSpecificNumberFromNumbersFromContactGroup) replyToSpecificNumber;
							replyToSpecificNumberStringBuilder
									.append(replyToSpecificNumberFromNumbersFromContactGroup
											.getName()
											+ " ("
											+ PhoneNumberUtils
													.formatNumber(replyToSpecificNumberFromNumbersFromContactGroup
															.getPhoneNumber())
											+ ") ");

						}
					}
				}
				responseToSpecificNumbersSummaryTextView
						.setText(replyToSpecificNumberStringBuilder.toString());

				TextView noneReplyListSummaryTextView = (TextView) view
						.findViewById(R.id.smsReplyAddEditNoneReplyListSummaryTextView);

				StringBuilder noneReplyNumberStringBuilder = new StringBuilder();
				if (smsReply.getNoneReplyNumbers() == null
						|| smsReply.getNoneReplyNumbers().size() == 0) {
					noneReplyNumberStringBuilder
							.append(getString(R.string.none));
				} else {
					boolean isAppend = false;
					String comma = ", ";
					for (NoneReplyNumber noneReplyNumber : smsReply
							.getNoneReplyNumbers()) {
						if (isAppend == false) {
							isAppend = true;
						} else {
							noneReplyNumberStringBuilder.append(comma);
						}
						if (noneReplyNumber instanceof NoneReplyNumberFromContacts) {
							noneReplyNumberStringBuilder
									.append(getString(R.string.sms_add_edit_none_reply_number_from_number_from_all_numbers_in_contacts));
						} else if (noneReplyNumber instanceof NoneReplyNumberFromContact) {
							NoneReplyNumberFromContact noneReplyNumberFromContact = (NoneReplyNumberFromContact) noneReplyNumber;
							noneReplyNumberStringBuilder
									.append(noneReplyNumberFromContact
											.getName()
											+ " ("
											+ PhoneNumberUtils
													.formatNumber(noneReplyNumberFromContact
															.getPhoneNumber())
											+ ") ");
						} else if (noneReplyNumber instanceof NoneReplyNumberFromUserInput) {
							NoneReplyNumberFromUserInput noneReplyNumberFromUserInput = (NoneReplyNumberFromUserInput) noneReplyNumber;
							noneReplyNumberStringBuilder
									.append(PhoneNumberUtils
											.formatNumber(noneReplyNumberFromUserInput
													.getPhoneNumber()));
						} else if (noneReplyNumber instanceof NoneReplyNumberFromMatchSmsWithKeyword) {
							NoneReplyNumberFromMatchSmsWithKeyword noneReplyNumberFromMatchSmsWithKeyword = (NoneReplyNumberFromMatchSmsWithKeyword) noneReplyNumber;
							noneReplyNumberStringBuilder
									.append(getString(R.string.sms_add_edit_none_reply_number_from_number_from_sms_messages_contains_keyword_keyword)
											+ noneReplyNumberFromMatchSmsWithKeyword
													.getKeyword());
						} else if (noneReplyNumber instanceof NoneReplyNumberFromNumbersFromContactGroup) {
							NoneReplyNumberFromNumbersFromContactGroup noneReplyNumberFromNumbersFromContactGroup = (NoneReplyNumberFromNumbersFromContactGroup) noneReplyNumber;
							noneReplyNumberStringBuilder
									.append(noneReplyNumberFromNumbersFromContactGroup
											.getName()
											+ " ("
											+ PhoneNumberUtils
													.formatNumber(noneReplyNumberFromNumbersFromContactGroup
															.getPhoneNumber())
											+ ") ");
						}
					}
				}
				noneReplyListSummaryTextView
						.setText(noneReplyNumberStringBuilder.toString());

				CheckBox enableDelayResponseForMissedCallCheckBox = (CheckBox) view
						.findViewById(R.id.smsReplyAddEditEnableDelayResponseToMissedCallCheckBox);
				TextView delayTimeForMissedCallSummaryTextView = (TextView) view
						.findViewById(R.id.smsReplyAddEditDelayTimeForMissedCallSummaryTextView);
				if (smsReply.getDelayTimeForNotResponseToRespondedMissedCall() == null) {
					smsReply.setDelayTimeForNotResponseToRespondedMissedCall(SmsReplyDelayTimeForMissedCallDialogFragment.ONE_MINUTE_IN_MILLISECONDS);
				}
				String delayTimeForMissedCallSummary = null;
				String[] delayTimeForMissedCallArray = getResources()
						.getStringArray(
								R.array.sms_reply_add_edit_delay_reply_missed_call_delay_time_array);
				if (smsReply.getDelayTimeForNotResponseToRespondedMissedCall() == SmsReplyDelayTimeForMissedCallDialogFragment.THIRTY_SECONDS_IN_MILLISECONDS) {
					delayTimeForMissedCallSummary = delayTimeForMissedCallArray[0];
				} else if (smsReply
						.getDelayTimeForNotResponseToRespondedMissedCall() == SmsReplyDelayTimeForMissedCallDialogFragment.FIVE_MINUTES_IN_MILLISECONDS) {
					delayTimeForMissedCallSummary = delayTimeForMissedCallArray[2];
				} else if (smsReply
						.getDelayTimeForNotResponseToRespondedMissedCall() == SmsReplyDelayTimeForMissedCallDialogFragment.TEN_MINUTES_IN_MILLISECONDS) {
					delayTimeForMissedCallSummary = delayTimeForMissedCallArray[3];
				} else {
					delayTimeForMissedCallSummary = delayTimeForMissedCallArray[1];
				}
				delayTimeForMissedCallSummaryTextView
						.setText(delayTimeForMissedCallSummary);
				RelativeLayout delayTimeForMissedCallRelativeLayout = (RelativeLayout) view
						.findViewById(R.id.smsReplyAddEditDelayTimeForMissedCallRelativeLayout);
				enableDelayResponseForMissedCallCheckBox.setChecked(smsReply
						.isEnableNotResponseToRespondedMissedCall());
				if (enableDelayResponseForMissedCallCheckBox.isChecked() == true) {
					delayTimeForMissedCallRelativeLayout
							.setVisibility(View.VISIBLE);
				} else {
					delayTimeForMissedCallRelativeLayout
							.setVisibility(View.GONE);
				}

				CheckBox enableDelayResponseForSmsCheckBox = (CheckBox) view
						.findViewById(R.id.smsReplyAddEditEnableDelayResponseCheckBox);
				TextView delayTimeForSmsSummaryTextView = (TextView) view
						.findViewById(R.id.smsReplyAddEditDelayTimeSummaryTextView);
				if (smsReply.getDelayTimeForNotResponseToReadMessage() == null) {
					smsReply.setDelayTimeForNotResponseToReadMessage(SmsReplyDelayTimeForSmsDialogFragment.THIRTY_SECONDS_IN_MILLISECONDS);
				}
				String delayTimeForSmsSummary = null;
				String[] delayTimeForSmsArray = getResources()
						.getStringArray(
								R.array.sms_reply_add_edit_delay_reply_sms_delay_time_array);
				if (smsReply.getDelayTimeForNotResponseToReadMessage() == SmsReplyDelayTimeForSmsDialogFragment.ONE_MINUTE_IN_MILLISECONDS) {
					delayTimeForSmsSummary = delayTimeForSmsArray[1];
				} else if (smsReply.getDelayTimeForNotResponseToReadMessage() == SmsReplyDelayTimeForSmsDialogFragment.FIVE_MINUTES_IN_MILLISECONDS) {
					delayTimeForSmsSummary = delayTimeForSmsArray[2];
				} else if (smsReply.getDelayTimeForNotResponseToReadMessage() == SmsReplyDelayTimeForSmsDialogFragment.TEN_MINUTES_IN_MILLISECONDS) {
					delayTimeForSmsSummary = delayTimeForSmsArray[3];
				} else {
					delayTimeForSmsSummary = delayTimeForSmsArray[0];
				}
				delayTimeForSmsSummaryTextView.setText(delayTimeForSmsSummary);
				RelativeLayout delayTimeForSmsRelativeLayout = (RelativeLayout) view
						.findViewById(R.id.smsReplyAddEditDelayTimeRelativeLayout);
				enableDelayResponseForSmsCheckBox.setChecked(smsReply
						.isEnableNotResponseToReadMessage());
				if (enableDelayResponseForSmsCheckBox.isChecked() == true) {
					delayTimeForSmsRelativeLayout.setVisibility(View.VISIBLE);
				} else {
					delayTimeForSmsRelativeLayout.setVisibility(View.GONE);
				}

				view.findViewById(R.id.smsReplyAddEditLoadingPanelLinearLayout)
						.setVisibility(View.GONE);
				view.findViewById(R.id.smsReplyAddEditContentScrollViewayout)
						.setVisibility(View.VISIBLE);
			}
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			View view = getView();
			view.findViewById(R.id.smsReplyAddEditContentScrollViewayout)
					.setVisibility(View.GONE);
			view.findViewById(R.id.smsReplyAddEditLoadingPanelLinearLayout)
					.setVisibility(View.VISIBLE);
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// To hook action bar menu
		setHasOptionsMenu(true);
		this.smsReplyService = new SmsReplyService(getActivity());
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getActivity().setTitle(
				getString(R.string.sms_reply_add_edit_menu_title));

		getActivity().getActionBar().setLogo(R.drawable.ic_action_accept);
		getActivity().getActionBar().setTitle(getString(R.string.done));
		getActivity().getActionBar().setDisplayOptions(
				ActionBar.DISPLAY_USE_LOGO | ActionBar.DISPLAY_SHOW_TITLE
						| ActionBar.DISPLAY_SHOW_HOME);
		long id = getArguments().getLong(ARGUMENTS_SMS_REPLY_ID);
		new LoadSmsReplyAsyncTask().execute(id);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.sms_reply_add_edit_actions, menu);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = getView();
		if (getView() == null) {
			view = inflater.inflate(R.layout.sms_reply_add_edit, container,
					false);
			final RelativeLayout responseToMissedCallRelativeLayoutPanel = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditMessageResponseToMissedCallRelativeLayoutPanel);
			final CheckBox responseToMissedCallCheckBox = (CheckBox) view
					.findViewById(R.id.smsReplyAddEditResponseToMissedCallCheckBox);
			responseToMissedCallRelativeLayoutPanel
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							responseToMissedCallCheckBox.performClick();
						}
					});
			responseToMissedCallCheckBox
					.setOnTouchListener(new View.OnTouchListener() {

						@Override
						public boolean onTouch(View v, MotionEvent event) {
							responseToMissedCallRelativeLayoutPanel
									.onTouchEvent(event);
							return true;
						}
					});
			responseToMissedCallCheckBox
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (smsReply != null) {
								smsReply.setShouldResponseToMissedCall(isChecked);
							}

						}
					});
			EditText titleEditText = (EditText) view
					.findViewById(R.id.smsReplyAddEditTitleEditText);
			titleEditText.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {

				}

				@Override
				public void afterTextChanged(Editable s) {
					if (smsReply != null) {
						smsReply.setTitle(s.toString());
					}
				}
			});
			final RelativeLayout responseToSmsRelativeLayoutPanel = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditMessageResponseToSmsRelativeLayoutPanel);
			final CheckBox responseToMSmsCheckBox = (CheckBox) view
					.findViewById(R.id.smsReplyAddEditResponseToSmsCheckBox);
			responseToSmsRelativeLayoutPanel
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							responseToMSmsCheckBox.performClick();
						}
					});
			responseToMSmsCheckBox
					.setOnTouchListener(new View.OnTouchListener() {

						@Override
						public boolean onTouch(View v, MotionEvent event) {
							responseToSmsRelativeLayoutPanel
									.onTouchEvent(event);
							return true;
						}
					});
			responseToMSmsCheckBox
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (smsReply != null) {
								smsReply.setShouldResponseToSms(isChecked);
							}
						}
					});
			RelativeLayout responseDurationSettingRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditResponseDurationSettingRelativeLayout);
			responseDurationSettingRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							SmsReplyReplyDateTimeDialogFragment smsReplyEditMessageDialogFragment = new SmsReplyReplyDateTimeDialogFragment();
							Bundle bundle = new Bundle();
							smsReplyEditMessageDialogFragment
									.setArguments(bundle);
							smsReplyEditMessageDialogFragment.show(
									getFragmentManager(),
									SmsReplyReplyDateTimeDialogFragment.TAG);

						}
					});
			RelativeLayout replyTimesRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditReplyTimesRelativeLayout);
			replyTimesRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							SmsReplyReplyTimesModeDialogFragment smsReplyReplyTimesModeDialogFragment = new SmsReplyReplyTimesModeDialogFragment();
							Bundle bundle = new Bundle();
							smsReplyReplyTimesModeDialogFragment
									.setArguments(bundle);
							smsReplyReplyTimesModeDialogFragment.show(
									getFragmentManager(),
									SmsReplyReplyTimesModeDialogFragment.TAG);

						}
					});

			final RelativeLayout silentModeRelativeLayoutPanel = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditSilentModeRelativeLayout);
			final CheckBox silentModeCheckBox = (CheckBox) view
					.findViewById(R.id.smsReplyAddEditSilentModeCheckBox);
			silentModeRelativeLayoutPanel
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							silentModeCheckBox.performClick();
						}
					});
			silentModeCheckBox.setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					silentModeRelativeLayoutPanel.onTouchEvent(event);
					return true;
				}
			});
			silentModeCheckBox
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (smsReply != null) {
								smsReply.setEnableRingerModeSilent(isChecked);
							}
						}
					});

			final RelativeLayout useDifferentMessageRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditUseDifferentMessageRelativeLayout);
			final CheckBox useDifferentMessageCheckBox = (CheckBox) view
					.findViewById(R.id.smsReplyAddEditUseDifferentMessageCheckBox);
			final RelativeLayout allMessageRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditAllMessageRelativeLayout);
			final RelativeLayout messageMissedCallRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditMessageMissedCallRelativeLayout);

			final RelativeLayout messageSmsRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditMessageSmsRelativeLayout);

			useDifferentMessageRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							useDifferentMessageCheckBox.performClick();
						}
					});
			useDifferentMessageCheckBox
					.setOnTouchListener(new View.OnTouchListener() {

						@Override
						public boolean onTouch(View v, MotionEvent event) {
							useDifferentMessageRelativeLayout
									.onTouchEvent(event);
							return true;
						}
					});
			useDifferentMessageCheckBox
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (smsReply != null) {
								smsReply.setMissedCallAndSmsUsedDifferentMessages(isChecked);
							}
							if (isChecked == true) {
								allMessageRelativeLayout
										.setVisibility(View.GONE);
								messageMissedCallRelativeLayout
										.setVisibility(View.VISIBLE);
								messageSmsRelativeLayout
										.setVisibility(View.VISIBLE);
							} else {
								allMessageRelativeLayout
										.setVisibility(View.VISIBLE);
								messageMissedCallRelativeLayout
										.setVisibility(View.GONE);
								messageSmsRelativeLayout
										.setVisibility(View.GONE);
							}

						}
					});
			allMessageRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							SmsReplyEditMessageDialogFragment smsReplyEditMessageDialogFragment = new SmsReplyEditMessageDialogFragment();
							Bundle bundle = new Bundle();
							bundle.putString(
									SmsReplyEditMessageDialogFragment.ARGEMENTS_MESSAGE,
									smsReply.getPhoneMessage());
							bundle.putSerializable(
									SmsReplyEditMessageDialogFragment.ARGEMENTS_MESSAGE_FOR,
									SmsReplyEditMessageDialogFragment.MessageFor.ALL);
							smsReplyEditMessageDialogFragment
									.setArguments(bundle);
							smsReplyEditMessageDialogFragment.show(
									getFragmentManager(),
									SmsReplyEditMessageDialogFragment.TAG);

						}
					});

			messageSmsRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							SmsReplyEditMessageDialogFragment smsReplyEditMessageDialogFragment = new SmsReplyEditMessageDialogFragment();
							Bundle bundle = new Bundle();
							bundle.putString(
									SmsReplyEditMessageDialogFragment.ARGEMENTS_MESSAGE,
									smsReply.getSmsMessage());
							bundle.putSerializable(
									SmsReplyEditMessageDialogFragment.ARGEMENTS_MESSAGE_FOR,
									SmsReplyEditMessageDialogFragment.MessageFor.SMS);
							smsReplyEditMessageDialogFragment
									.setArguments(bundle);
							smsReplyEditMessageDialogFragment.show(
									getFragmentManager(),
									SmsReplyEditMessageDialogFragment.TAG);

						}
					});

			messageMissedCallRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							SmsReplyEditMessageDialogFragment smsReplyEditMessageDialogFragment = new SmsReplyEditMessageDialogFragment();
							Bundle bundle = new Bundle();
							bundle.putString(
									SmsReplyEditMessageDialogFragment.ARGEMENTS_MESSAGE,
									smsReply.getPhoneMessage());
							bundle.putSerializable(
									SmsReplyEditMessageDialogFragment.ARGEMENTS_MESSAGE_FOR,
									SmsReplyEditMessageDialogFragment.MessageFor.MISSED_CALL);
							smsReplyEditMessageDialogFragment
									.setArguments(bundle);
							smsReplyEditMessageDialogFragment.show(
									getFragmentManager(),
									SmsReplyEditMessageDialogFragment.TAG);

						}
					});

			final RelativeLayout replyModeSettingRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditReplyModeSettingRelativeLayout);
			final CheckBox replyToSpecificNumberCheckBox = (CheckBox) view
					.findViewById(R.id.smsReplyAddEditReplyToSpecificNumberCheckBox);
			replyModeSettingRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							replyToSpecificNumberCheckBox.performClick();
						}
					});
			replyToSpecificNumberCheckBox
					.setOnTouchListener(new View.OnTouchListener() {

						@Override
						public boolean onTouch(View v, MotionEvent event) {
							replyModeSettingRelativeLayout.onTouchEvent(event);
							return true;
						}
					});

			RelativeLayout numberFromRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditNumberFromRelativeLayout);
			numberFromRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							Fragment newFragment = new SmsReplyResponseNumberFromListFragment();
							FragmentTransaction transaction = getActivity()
									.getFragmentManager().beginTransaction();

							transaction
									.setCustomAnimations(
											R.animator.right_slide_enter_object_animator,
											R.animator.right_slide_exist_object_animator,
											R.animator.left_slide_enter_object_animator,
											R.animator.left_slide_exist_object_animator);
							Bundle bundle = new Bundle();
							bundle.putSerializable(
									SmsReplyResponseNumberFromListFragment.ARGUMENTS_SMS_REPLY_RESPONSE_LIST,
									(Serializable) smsReply
											.getResponseNumberFroms());
							newFragment.setArguments(bundle);
							transaction.replace(
									R.id.sms_reply_add_edit_main_content_frame,
									newFragment);
							transaction.addToBackStack(null);

							transaction.commit();
						}
					});
			final RelativeLayout reponseToSpecificNumberRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditResponseToSpecificNumbersRelativeLayout);

			reponseToSpecificNumberRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							Fragment newFragment = new SmsReplyReplyToSpecificNumberFromListFragment();
							FragmentTransaction transaction = getActivity()
									.getFragmentManager().beginTransaction();

							transaction
									.setCustomAnimations(
											R.animator.right_slide_enter_object_animator,
											R.animator.right_slide_exist_object_animator,
											R.animator.left_slide_enter_object_animator,
											R.animator.left_slide_exist_object_animator);
							Bundle bundle = new Bundle();
							bundle.putSerializable(
									SmsReplyReplyToSpecificNumberFromListFragment.ARGUMENTS_SMS_REPLY_REPLY_TO_SPECIFIC_LIST,
									(Serializable) smsReply
											.getReplyToSpecificNumbers());
							newFragment.setArguments(bundle);
							transaction.replace(
									R.id.sms_reply_add_edit_main_content_frame,
									newFragment);
							transaction.addToBackStack(null);

							transaction.commit();

						}
					});
			replyToSpecificNumberCheckBox
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (smsReply != null) {
								smsReply.setReplyMode(isChecked ? SmsReply.REPLY_MODE_VALUE_REPLY_TO_SPECIFIC_NUMBERS
										: SmsReply.REPLY_MODE_VALUE_REPLY_DIRECTLY);
							}
							if (isChecked == true) {
								reponseToSpecificNumberRelativeLayout
										.setVisibility(View.VISIBLE);
							} else {
								reponseToSpecificNumberRelativeLayout
										.setVisibility(View.GONE);
							}

						}
					});
			final RelativeLayout noneReplyListRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditNoneReplyListRelativeLayout);
			noneReplyListRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							Fragment newFragment = new SmsReplyNoneReplyNumberFromListFragment();
							FragmentTransaction transaction = getActivity()
									.getFragmentManager().beginTransaction();

							transaction
									.setCustomAnimations(
											R.animator.right_slide_enter_object_animator,
											R.animator.right_slide_exist_object_animator,
											R.animator.left_slide_enter_object_animator,
											R.animator.left_slide_exist_object_animator);
							Bundle bundle = new Bundle();
							bundle.putSerializable(
									SmsReplyNoneReplyNumberFromListFragment.ARGUMENTS_SMS_REPLY_NONE_REPLY_LIST,
									(Serializable) smsReply
											.getNoneReplyNumbers());
							newFragment.setArguments(bundle);
							transaction.replace(
									R.id.sms_reply_add_edit_main_content_frame,
									newFragment);
							transaction.addToBackStack(null);

							transaction.commit();

						}
					});

			final RelativeLayout enableDelayResponseForMissedCallRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditEnableDelayResponseToMissedCallRelativeLayout);
			final CheckBox enableDelayResponseForMissedCallCheckBox = (CheckBox) view
					.findViewById(R.id.smsReplyAddEditEnableDelayResponseToMissedCallCheckBox);
			final RelativeLayout delayTimeForMissedCallRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditDelayTimeForMissedCallRelativeLayout);
			enableDelayResponseForMissedCallRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							enableDelayResponseForMissedCallCheckBox
									.performClick();
						}
					});
			enableDelayResponseForMissedCallCheckBox
					.setOnTouchListener(new View.OnTouchListener() {

						@Override
						public boolean onTouch(View v, MotionEvent event) {
							enableDelayResponseForMissedCallRelativeLayout
									.onTouchEvent(event);
							return true;
						}
					});
			enableDelayResponseForMissedCallCheckBox
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (smsReply != null) {
								smsReply.setEnableNotResponseToRespondedMissedCall(isChecked);
							}
							if (isChecked == true) {
								delayTimeForMissedCallRelativeLayout
										.setVisibility(View.VISIBLE);
							} else {
								delayTimeForMissedCallRelativeLayout
										.setVisibility(View.GONE);
							}

						}
					});
			delayTimeForMissedCallRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							SmsReplyDelayTimeForMissedCallDialogFragment smsReplyDelayTimeDialogFragment = new SmsReplyDelayTimeForMissedCallDialogFragment();
							Bundle bundle = new Bundle();
							bundle.putLong(
									SmsReplyDelayTimeForMissedCallDialogFragment.ARGUMENTS_DELAY_TIME,
									smsReply.getDelayTimeForNotResponseToRespondedMissedCall() == null ? SmsReplyDelayTimeForMissedCallDialogFragment.THIRTY_SECONDS_IN_MILLISECONDS
											: smsReply
													.getDelayTimeForNotResponseToRespondedMissedCall());
							smsReplyDelayTimeDialogFragment
									.setArguments(bundle);
							smsReplyDelayTimeDialogFragment
									.show(getFragmentManager(),
											SmsReplyDelayTimeForMissedCallDialogFragment.TAG);

						}
					});
			final RelativeLayout enableDelayResponseForSmsRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditEnableDelayResponseRelativeLayout);
			final CheckBox enableDelayResponseForSmsCheckBox = (CheckBox) view
					.findViewById(R.id.smsReplyAddEditEnableDelayResponseCheckBox);
			final RelativeLayout delayTimeForSmsRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.smsReplyAddEditDelayTimeRelativeLayout);
			enableDelayResponseForSmsRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							enableDelayResponseForSmsCheckBox.performClick();
						}
					});
			enableDelayResponseForSmsCheckBox
					.setOnTouchListener(new View.OnTouchListener() {

						@Override
						public boolean onTouch(View v, MotionEvent event) {
							enableDelayResponseForSmsRelativeLayout
									.onTouchEvent(event);
							return true;
						}
					});
			enableDelayResponseForSmsCheckBox
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (smsReply != null) {
								smsReply.setEnableNotResponseToReadMessage(isChecked);
							}
							if (isChecked == true) {
								delayTimeForSmsRelativeLayout
										.setVisibility(View.VISIBLE);
							} else {
								delayTimeForSmsRelativeLayout
										.setVisibility(View.GONE);
							}

						}
					});
			delayTimeForSmsRelativeLayout
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							SmsReplyDelayTimeForSmsDialogFragment smsReplyDelayTimeDialogFragment = new SmsReplyDelayTimeForSmsDialogFragment();
							Bundle bundle = new Bundle();
							bundle.putLong(
									SmsReplyDelayTimeForSmsDialogFragment.ARGUMENTS_DELAY_TIME,
									smsReply.getDelayTimeForNotResponseToReadMessage() == null ? SmsReplyDelayTimeForSmsDialogFragment.THIRTY_SECONDS_IN_MILLISECONDS
											: smsReply
													.getDelayTimeForNotResponseToReadMessage());
							smsReplyDelayTimeDialogFragment
									.setArguments(bundle);
							smsReplyDelayTimeDialogFragment.show(
									getFragmentManager(),
									SmsReplyDelayTimeForSmsDialogFragment.TAG);

						}
					});
		}

		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.smsReply = null;
		this.smsReplyService = null;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			getActivity().onBackPressed();
			return true;
		case R.id.menu_sms_reply_add_edit_discard:
			getActivity().finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void updateMessage(
			SmsReplyEditMessageDialogFragment.MessageFor messageFor,
			String newMssage) {
		if (SmsReplyEditMessageDialogFragment.MessageFor.ALL.equals(messageFor)
				|| SmsReplyEditMessageDialogFragment.MessageFor.MISSED_CALL
						.equals(messageFor)) {
			this.smsReply.setPhoneMessage(newMssage);

			((TextView) getActivity().findViewById(
					R.id.smsReplyAddEditAllMessageSummaryTextView))
					.setText(this.smsReply.getPhoneMessage());

			((TextView) getActivity()
					.findViewById(
							R.id.smsReplyAddEditMessageMissedCallMessageSummaryTextView))
					.setText(this.smsReply.getPhoneMessage());
		} else if (SmsReplyEditMessageDialogFragment.MessageFor.SMS
				.equals(messageFor)) {
			this.smsReply.setSmsMessage(newMssage);

			((TextView) getActivity().findViewById(
					R.id.smsReplyAddEditMessageSmsMessageSummaryTextView))
					.setText(this.smsReply.getSmsMessage());
		}
	}

	public void handleReplyDateTimeType(
			SmsReplyReplyDateTimeDialogFragment.ReplyDateTimeType replyDateTimeType) {
		if (SmsReplyReplyDateTimeDialogFragment.ReplyDateTimeType.ALWAYS
				.equals(replyDateTimeType)) {
			SmsReplyReplyDateTimeAlwaysFragment replyReplyDateTimeAlwaysFragment = new SmsReplyReplyDateTimeAlwaysFragment();

			Bundle bundle = new Bundle();
			bundle.putSerializable(
					SmsReplyReplyDateTimeAlwaysFragment.ARGUMENTS_REPLY_DATE_TIME_ALWAYS,
					this.smsReply);
			replyReplyDateTimeAlwaysFragment.setArguments(bundle);
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager
					.beginTransaction()
					.add(replyReplyDateTimeAlwaysFragment,
							SmsReplyReplyDateTimeAlwaysFragment.TAG).commit();
		} else if (SmsReplyReplyDateTimeDialogFragment.ReplyDateTimeType.EXPIRED_AFTER
				.equals(replyDateTimeType)) {
			SmsReplyReplyDateTimeExpiredAfterDialogFragment smsReplyReplyDateTimeExpiredAfterDialogFragment = new SmsReplyReplyDateTimeExpiredAfterDialogFragment();
			Bundle bundle = new Bundle();
			bundle.putSerializable(
					SmsReplyReplyDateTimeExpiredAfterDialogFragment.ARGUMENTS_REPLY_DATE_TIME_EXPIRED_AFTER,
					this.smsReply);
			smsReplyReplyDateTimeExpiredAfterDialogFragment
					.setArguments(bundle);
			smsReplyReplyDateTimeExpiredAfterDialogFragment.show(
					getFragmentManager(),
					SmsReplyReplyDateTimeExpiredAfterDialogFragment.TAG);
		} else if (SmsReplyReplyDateTimeDialogFragment.ReplyDateTimeType.PERIOD
				.equals(replyDateTimeType)) {
			SmsReplyReplyDateTimePeriodDialogFragment smsReplyReplyDateTimeExpiredPeriodDialogFragment = new SmsReplyReplyDateTimePeriodDialogFragment();
			Bundle bundle = new Bundle();
			bundle.putSerializable(
					SmsReplyReplyDateTimePeriodDialogFragment.ARGUMENTS_REPLY_DATE_TIME_PERIOD,
					this.smsReply);
			smsReplyReplyDateTimeExpiredPeriodDialogFragment
					.setArguments(bundle);
			smsReplyReplyDateTimeExpiredPeriodDialogFragment.show(
					getFragmentManager(),
					SmsReplyReplyDateTimePeriodDialogFragment.TAG);
		} else if (SmsReplyReplyDateTimeDialogFragment.ReplyDateTimeType.REPEAT
				.equals(replyDateTimeType)) {
			SmsReplyReplyDateTimeRepeatDialogFragment smsReplyReplyDateTimeRepeatDialogFragment = new SmsReplyReplyDateTimeRepeatDialogFragment();
			Bundle bundle = new Bundle();
			bundle.putSerializable(
					SmsReplyReplyDateTimeRepeatDialogFragment.ARGUMENTS_REPLY_DATE_TIME_REPEAT,
					this.smsReply);
			smsReplyReplyDateTimeRepeatDialogFragment.setArguments(bundle);
			smsReplyReplyDateTimeRepeatDialogFragment.show(
					getFragmentManager(),
					SmsReplyReplyDateTimeRepeatDialogFragment.TAG);
		}
	}

	public void handleReplyDateTimeMode(SmsReply smsReply) {
		TextView responseDurationSettingSummaryTextView = (TextView) getActivity()
				.findViewById(
						R.id.smsReplyAddEditResponseDurationSettingSummaryTextView);
		if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_ALWAYS_TYPE) {
			smsReply.setReplyDateTimeModeType(SmsReply.REPLY_DATE_TIME_MODE_ALWAYS_TYPE);
			responseDurationSettingSummaryTextView
					.setText(R.string.sms_reply_add_edit_date_time_summary_always);
			FragmentManager fragmentManager = getActivity()
					.getFragmentManager();
			fragmentManager
					.beginTransaction()
					.remove(fragmentManager
							.findFragmentByTag(SmsReplyReplyDateTimeAlwaysFragment.TAG))
					.commit();
		} else if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_EXPIRED_AFTER_TYPE) {
			smsReply.setReplyDateTimeModeType(SmsReply.REPLY_DATE_TIME_MODE_EXPIRED_AFTER_TYPE);
			long hours = (smsReply.getExpiredAfterMilliSeconds() / 1000l / 60 / 60);
			long minutes = (smsReply.getExpiredAfterMilliSeconds() % (1000l * 60 * 60)) / 60000;
			responseDurationSettingSummaryTextView
					.setText(getString(
							R.string.sms_reply_add_edit_date_time_summary_expired_after,
							hours < 10 ? "0" + hours : hours,
							minutes < 10 ? "0" + minutes : minutes));
		} else if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_PERIOD_TYPE) {
			smsReply.setReplyDateTimeModeType(SmsReply.REPLY_DATE_TIME_MODE_PERIOD_TYPE);
			responseDurationSettingSummaryTextView.setText(getString(
					R.string.sms_reply_add_edit_date_time_summary_period,
					DateFormat.getDateFormat(getActivity()).format(
							smsReply.getPeriodStartDateTime()),
					DateFormat.getTimeFormat(getActivity()).format(
							smsReply.getPeriodStartDateTime()),
					DateFormat.getDateFormat(getActivity()).format(
							smsReply.getPeriodEndDateTime()),
					DateFormat.getTimeFormat(getActivity()).format(
							smsReply.getPeriodEndDateTime())));
		} else if (smsReply.getReplyDateTimeModeType() == SmsReply.REPLY_DATE_TIME_MODE_REPEAT_TYPE) {
			smsReply.setReplyDateTimeModeType(SmsReply.REPLY_DATE_TIME_MODE_REPEAT_TYPE);
			StringBuilder stringBuilder = new StringBuilder();
			if (DateTimeUtil.isNeverRepeat(smsReply) == true) {
				stringBuilder.append(getString(R.string.never));
			} else {
				boolean isAppended = false;
				String comma = getActivity().getString(R.string.comma);
				if (smsReply.isSundayEnable() == true) {
					if (isAppended == false) {
						isAppended = true;
					} else {
						stringBuilder.append(comma);
					}
					stringBuilder.append(this.getActivity().getString(
							R.string.sunday_abbreviation));
				}
				if (smsReply.isMondayEnable() == true) {
					if (isAppended == false) {
						isAppended = true;
					} else {
						stringBuilder.append(comma);
					}
					stringBuilder.append(this.getActivity().getString(
							R.string.monday_abbreviation));

				}
				if (smsReply.isTuesdayEnable() == true) {
					if (isAppended == false) {
						isAppended = true;
					} else {
						stringBuilder.append(comma);
					}
					stringBuilder.append(this.getActivity().getString(
							R.string.tuesday_abbreviation));

				}
				if (smsReply.isWednesdayEnable() == true) {
					if (isAppended == false) {
						isAppended = true;
					} else {
						stringBuilder.append(comma);
					}
					stringBuilder.append(this.getActivity().getString(
							R.string.wednesday_abbreviation));

				}
				if (smsReply.isThursdayEnable() == true) {
					if (isAppended == false) {
						isAppended = true;
					} else {
						stringBuilder.append(comma);
					}
					stringBuilder.append(this.getActivity().getString(
							R.string.thursday_abbreviation));
				}
				if (smsReply.isFridayEnable() == true) {
					if (isAppended == false) {
						isAppended = true;
					} else {
						stringBuilder.append(comma);
					}
					stringBuilder.append(this.getActivity().getString(
							R.string.friday_abbreviation));

				}
				if (smsReply.isSaturdayEnable() == true) {
					if (isAppended == false) {
						isAppended = true;
					} else {
						stringBuilder.append(comma);
					}
					stringBuilder.append(this.getActivity().getString(
							R.string.saturday_abbreviation));

				}
			}
			responseDurationSettingSummaryTextView.setText(getString(
					R.string.sms_reply_add_edit_date_time_summary_repeat,
					DateFormat.getTimeFormat(getActivity()).format(
							smsReply.getRepeatStartTime()),
					DateFormat.getTimeFormat(getActivity()).format(
							smsReply.getRepeatEndTime()), stringBuilder
							.toString()));
		}

	}

	public void handleChooseDelayTimeForMissedCall(long delayTime) {
		this.smsReply
				.setDelayTimeForNotResponseToRespondedMissedCall(delayTime);
		TextView delayTimeSummaryTextView = (TextView) getView().findViewById(
				R.id.smsReplyAddEditDelayTimeForMissedCallSummaryTextView);
		String delayTimeSummary = null;
		String[] delayTimeArray = getResources()
				.getStringArray(
						R.array.sms_reply_add_edit_delay_reply_missed_call_delay_time_array);
		if (smsReply.getDelayTimeForNotResponseToRespondedMissedCall() == SmsReplyDelayTimeForSmsDialogFragment.ONE_MINUTE_IN_MILLISECONDS) {
			delayTimeSummary = delayTimeArray[0];
		} else if (smsReply.getDelayTimeForNotResponseToRespondedMissedCall() == SmsReplyDelayTimeForSmsDialogFragment.FIVE_MINUTES_IN_MILLISECONDS) {
			delayTimeSummary = delayTimeArray[2];
		} else if (smsReply.getDelayTimeForNotResponseToRespondedMissedCall() == SmsReplyDelayTimeForSmsDialogFragment.TEN_MINUTES_IN_MILLISECONDS) {
			delayTimeSummary = delayTimeArray[3];
		} else {
			delayTimeSummary = delayTimeArray[1];
		}
		delayTimeSummaryTextView.setText(delayTimeSummary);
	}

	public void handleChooseDelayTimeForSms(long delayTime) {
		this.smsReply.setDelayTimeForNotResponseToReadMessage(delayTime);
		TextView delayTimeSummaryTextView = (TextView) getView().findViewById(
				R.id.smsReplyAddEditDelayTimeSummaryTextView);
		String delayTimeSummary = null;
		String[] delayTimeArray = getResources().getStringArray(
				R.array.sms_reply_add_edit_delay_reply_sms_delay_time_array);
		if (smsReply.getDelayTimeForNotResponseToReadMessage() == SmsReplyDelayTimeForSmsDialogFragment.ONE_MINUTE_IN_MILLISECONDS) {
			delayTimeSummary = delayTimeArray[1];
		} else if (smsReply.getDelayTimeForNotResponseToReadMessage() == SmsReplyDelayTimeForSmsDialogFragment.FIVE_MINUTES_IN_MILLISECONDS) {
			delayTimeSummary = delayTimeArray[2];
		} else if (smsReply.getDelayTimeForNotResponseToReadMessage() == SmsReplyDelayTimeForSmsDialogFragment.TEN_MINUTES_IN_MILLISECONDS) {
			delayTimeSummary = delayTimeArray[3];
		} else {
			delayTimeSummary = delayTimeArray[0];
		}
		delayTimeSummaryTextView.setText(delayTimeSummary);
	}

	public void handleChooseReplyTimesAlwaysReply() {
		for (ReplyTimesMode replyTimesMode : this.smsReply.getReplyTimesModes()) {
			if (replyTimesMode instanceof ReplyTimesModeAlways) {
				replyTimesMode.setIsActivated(true);
			} else {
				replyTimesMode.setIsActivated(false);
			}
		}

		TextView smsReplyAddEditReplyTimesSummaryTextView = (TextView) getView()
				.findViewById(R.id.smsReplyAddEditReplyTimesSummaryTextView);
		smsReplyAddEditReplyTimesSummaryTextView
				.setText(R.string.sms_reply_add_edit_reply_times_always_reply);
	}

	public void handleChooseReplyTimesLimitedTimes() {
		int times = 0;
		for (ReplyTimesMode replyTimesMode : this.smsReply.getReplyTimesModes()) {
			if (replyTimesMode instanceof ReplyTimesModeLimitedTimes) {
				ReplyTimesModeLimitedTimes replyTimesModeLimitedTimes = (ReplyTimesModeLimitedTimes) replyTimesMode;
				times = replyTimesModeLimitedTimes.getTimes();
			}
		}
		if (times < 1) {
			times = 5;
		}
		SmsReplyReplyTimesModeLimitedTimesDialogFragment smsReplyEditMessageDialogFragment = new SmsReplyReplyTimesModeLimitedTimesDialogFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(
				SmsReplyReplyTimesModeLimitedTimesDialogFragment.ARGUMENTS_REPLY_TIMES_MODE_LIMITED_TIMES,
				times);
		smsReplyEditMessageDialogFragment.setArguments(bundle);
		smsReplyEditMessageDialogFragment.show(getFragmentManager(),
				SmsReplyReplyTimesModeLimitedTimesDialogFragment.TAG);
	}

	public void handleChooseTimesOnReplyTimesLimitedTimesDialog(int times) {
		if (times < 1) {
			times = 5;
		}
		for (ReplyTimesMode replyTimesMode : this.smsReply.getReplyTimesModes()) {
			if (replyTimesMode instanceof ReplyTimesModeLimitedTimes) {
				ReplyTimesModeLimitedTimes replyTimesModeLimitedTimes = (ReplyTimesModeLimitedTimes) replyTimesMode;
				replyTimesModeLimitedTimes.setTimes(times);
				replyTimesModeLimitedTimes.setIsActivated(true);
			} else {
				replyTimesMode.setIsActivated(false);
			}
		}

		TextView smsReplyAddEditReplyTimesSummaryTextView = (TextView) getView()
				.findViewById(R.id.smsReplyAddEditReplyTimesSummaryTextView);
		smsReplyAddEditReplyTimesSummaryTextView.setText(getResources()
				.getQuantityString(
						R.plurals.sms_reply_add_edit_reply_times_limited_times,
						times, times));
	}

	public boolean insertOrUpdateSmsReply() {
		try {
			this.smsReplyService.insertOrUpdate(smsReply);
			if (smsReply.getId() != null) {
				Toast.makeText(
						getActivity(),
						getString(R.string.sms_reply_add_edit_update_settings,
								this.smsReply.getTitle().replace("\r\n", "")),
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(
						getActivity(),
						getString(
								R.string.sms_reply_add_edit_create_new_settings,
								this.smsReply.getTitle().replace("\r\n", "")),
						Toast.LENGTH_SHORT).show();
			}
		} catch (RemoteException e) {
			Log.e(LOG_TAG, "RemoteException", e);
			return false;
		} catch (OperationApplicationException e) {
			Log.e(LOG_TAG, "OperationApplicationException", e);
			return false;
		}
		return true;
	}
}
